package com.lzt.app

import org.json4s.{DefaultFormats, Formats, JValue}
import org.scalatra.ScalatraServlet
import org.scalatra.json.JacksonJsonSupport
import org.slf4j.{Logger, LoggerFactory}
import org.json4s.JsonDSL._


class NodeServlet extends ScalatraServlet with JacksonJsonSupport {

  private val logger: Logger = LoggerFactory.getLogger(classOf[NodeServlet])
  protected implicit lazy val jsonFormats: Formats = DefaultFormats

  before() {
    contentType = formats("json")
  }

  /**
    * 返回最后一次的延时数据，以当前时间的整分钟数计算，前一分钟到当前分钟的数据
    */
  get("/delay") {
    val date = request.getParameter("date")
    val service = request.getParameter("service")
    logger.debug("request date:%s, service:%s".format(date, service))

    var args: Tuple2[String, String] = null
    if (StringUtil.isNull(date)) {
      args = StringUtil.getLastMinute()
    } else {
      args = StringUtil.getLastMinute(StringUtil.parseDate(date))
    }

    import HbaseTemplate.convertJson

    val result = HbaseTemplate.scan[JValue]("tccgl_ping", args._1, args._2)
    logger.debug("系统最后一次网络探测：{}", result)

    compact(result)
  }
}